package com.xmmj.baselibrary.base.activity.swipeback

import android.view.ViewGroup
import android.widget.FrameLayout
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.content.Context
import android.graphics.Canvas
import android.view.View
import java.lang.ref.WeakReference

class TemporaryViewGroup(context: Context) : FrameLayout(context) {
    private val mShadowDrawable: Drawable

    private val mScreenWidth: Int

    private var mPreviousView: WeakReference<View?>? = null

    private var mStatusBarOffset = 0

    //px 当前滑动距离 （正数或0）
    private var mDistanceX = 0f

    companion object {
        private const val SHADOW_WIDTH = 50 //px 阴影宽度
    }

    init {
        val colors = intArrayOf(0x00000000, 0x17000000, 0x43000000) //分别为开始颜色，中间夜色，结束颜色
        mShadowDrawable = GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors)
        mScreenWidth = getContext().resources.displayMetrics.widthPixels
    }

    fun addPreviousView(
        currentContentView: ViewGroup?,
        previousContentView: ViewGroup,
        previousView: View
    ) {
        mPreviousView = WeakReference(previousView)
        val height = context.resources.displayMetrics.heightPixels
        val previousViewHeight = previousContentView.measuredHeight
        val currentViewHeight = currentContentView!!.measuredHeight
        val isCurrentFull = currentViewHeight == height
        val isPreviousFull = previousViewHeight == height
        mStatusBarOffset = if (isCurrentFull) {
            if (isPreviousFull) 0 else height - previousViewHeight
        } else {
            if (isPreviousFull) -(height - currentViewHeight) else 0
        }
        val previousParams = previousView.layoutParams as MarginLayoutParams
        previousParams.topMargin = previousParams.topMargin + mStatusBarOffset
        addView(previousView, 0, previousParams)
        previousView.translationX = -mScreenWidth.toFloat() / 3
    }

    fun clearPreviousView(previewContentView: ViewGroup?, previousView: View?) {
        val previousParams = previousView!!.layoutParams as MarginLayoutParams
        previousParams.topMargin = previousParams.topMargin - mStatusBarOffset
        removeView(previousView)
        previewContentView?.addView(previousView, previousParams)
    }

    fun setDistanceX(distanceX: Float) {
        mDistanceX = distanceX
        invalidate()
    }

    override fun drawChild(canvas: Canvas, child: View, drawingTime: Long): Boolean {
        if (mDistanceX < mScreenWidth && mPreviousView != null && mPreviousView!!.get() === child) {
            // draw previous view
            val restoreCount = canvas.save()
            canvas.clipRect(0f, 0f, mDistanceX, measuredHeight.toFloat())
            val result = super.drawChild(canvas, child, drawingTime)

            // draw shadow view
            canvas.translate(mDistanceX - SHADOW_WIDTH, 0f)
            mShadowDrawable.setBounds(0, 0, SHADOW_WIDTH, measuredHeight)
            mShadowDrawable.draw(canvas)
            canvas.restoreToCount(restoreCount)
            return result
        }
        return super.drawChild(canvas, child, drawingTime)
    }

    override fun dispatchDraw(canvas: Canvas) {
        super.dispatchDraw(canvas)
        // draw cached view
        if (mDistanceX >= mScreenWidth && mPreviousView != null && mPreviousView!!.get() != null) {
            val restoreCount = canvas.save()
            canvas.translate(0f, mStatusBarOffset.toFloat())
            mPreviousView!!.get()!!.draw(canvas)
            canvas.restoreToCount(restoreCount)
        }
    }
}